import {
	getSort,
	getMutipSort
} from "@/utils/functions";
import Vue from "vue";
import {
	chatData
} from "@/utils/data/chatData";
import {
	chatUtils
} from "@/utils/my/chatUtils";
import {
	lastUtils
} from "@/utils/my/lastChatUtils";
import {
	msgConst
} from "@/utils/my/const/msgConst";
import {
	roomService
} from "@/service/roomService";
import TalkEvent from "@/plugins/socket/event/talk-event";

function getTalkText(msg) {
	let text = msg.content;
	switch (msg.type) {
		case msgConst.image:
			text = "[图片消息]";
			break;
		case msgConst.voice:
			text = "[语音消息]";
			break;
		case msgConst.location:
			text = "[会话记录]";
			break;
		case msgConst.video:
			text = "[视频消息]";
			break;
		case msgConst.file:
			text = "[文件消息]";
			break;
		case msgConst.IMAGE_TEXT:
			text = "[图片消息]";
			break;
	}
	return text;
}

const Talk = {
	state: {
		// 用户对话列表
		items: [],

		// 最后一条消息
		unreadMessage: {
			num: 0,
			nickname: "未知",
			content: "..."
		},

		// 对话列表重载状态
		heavyLoad: false
	},
	getters: {
		// 过滤所有置顶对话列表
		topItems: state => {
			return state.items.filter(item => item.isTop == 1);
		},
		talkItems: state => {
			return state.items.sort(
				getMutipSort([
					getSort((a, b) => a.isTop > b.isTop),
					getSort((a, b) => a.unreadNum > b.unreadNum),
					getSort((a, b) => a.updateTime > b.updateTime)
				])
			);
		},
		// 消息未读数总计
		unreadNum: state => {
			return state.items.reduce((total, item) => {
				return total + parseInt(item.unreadNum);
			}, 0);
		},
		talkNum: state => state.items.length
	},
	mutations: {
		unread_add(state, {
			chatId,
			count
		}) {
			state.items.forEach((chat, index) => {
				if (chat.chatId == chatId) {
					state.items[index].unreadNum += count;
				}
			});
		},
		unread_clear(state, chatId) {
			state.items.forEach((chat, index) => {
				if (chat.chatId == chatId) {
					state.items[index].unreadNum = 0;
				}
			});
		},
		// 设置对话列表
		chats_init(state, data) {
			let items = [];
			data.forEach((item, index) => {
				item.content = getTalkText(item);
				let format = lastUtils.formatTalkItem(item);
				if (format) {
					items.push(format);
				}
			});

			items.pushAllFilter(chatData.listNew(), "chatId");
			state.items = items;
			console.log(items);
		},
		del_draft(state, chatId) {
			state.items.forEach((chat, index) => {
				if (chat.chatId == chatId) {
					state.items[index].draftText = "";
				}
			});
		},
		update_chat_name(state, {
			chatId,
			name
		}) {
			state.items.forEach((chat, index) => {
				if (chat.chatId == chatId) {
					state.items[index].name = name;
				}
			});
		},
		update_chat_avatar(state, {
			chatId,
			avatar
		}) {
			state.items.forEach((chat, index) => {
				if (chat.chatId == chatId) {
					state.items[index].avatar = avatar;
				}
			});
		},
		// 更新对话节点
		UPDATE_TALK_ITEM(state, resource) {
			Object.assign(state.items[resource.index], resource.item);
		},
		// 新增对话节点
		INSERT_TALK_ITEM(state, resource) {
			if (resource) {
				if (!chatUtils.hasChat(resource.chatId)) {
					state.items.push(resource);
				}
			}
		},

		// 移除对话节点
		del_chat(state, chatId) {
			if (chatUtils.isCurrentChat(chatId)) {
				this.commit("update_current_chatId", null);
			}
			state.items.delByFields("chatId", chatId);
		},

		// 更新对话节点在线状态
		UPDATE_TALK_ONLINE_STATUS(state, resource) {
			state.items[resource.index].online = parseInt(resource.status);
		},
		// 更新节点
		update_chat(state, {
			chatId,
			key,
			value
		}) {
			state.items.forEach(function(item, index) {
				if (item.chatId == chatId) {
					// Vue.set(item, key, value);
					state.items[index][key] = value;
					// item[key] = value
				}
			});
		},
		// 更新对话消息
		UPDATE_TALK_MESSAGE(state, resource) {
			state.items[resource.index].content = resource.item.content;
			// state.items[resource.index].unreadNum++
			state.items[resource.index].updateTime = resource.item.updateTime;
		},

		// 触发对话列表重新加载
		TRIGGER_TALK_ITEMS_LOAD(state, status = false) {
			state.heavyLoad = status;
		},

		SET_TLAK_UNREAD_MESSAGE(state, resource) {
			state.unreadMessage.num++;
			state.unreadMessage.nickname = resource.nickname;
			state.unreadMessage.content = resource.content;
		},

		// 清除最后一条未读消息
		CLEAR_TLAK_UNREAD_MESSAGE(state) {
			state.unreadMessage = {
				num: 0,
				nickname: "未知",
				content: "..."
			};
		}
	}
};

export default Talk;
